package com.wehealth;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.support.v4.app.Fragment;
import android.text.Html;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.wehealth.Plan_Day;
import com.wehealth.adapter.DrListAdapter;
import com.wehealth.parse.ParseJson;
import com.wehealth.util.DoTask;

public class Plan_Week extends Fragment {
	
	//SIMMON_ADD
	private MainActivity activity;
	private static  String JSONResult;
	private int[] mProgress = new int[7];
	private ProgressBar[] bGoWeek = new ProgressBar[7];
	private TextView[] mProgressValue = new TextView[7];

	public  Plan_Week()
	{
		Log.i("init:", "plan_week"); 
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		return  inflater.inflate(R.layout.plan_week, null);
	}
	
	
	
	public void onActivityCreated(Bundle savedInstanceState){
		
		super.onActivityCreated(savedInstanceState);
		
		
		 activity = (MainActivity)getActivity();
		 MobclickAgent.openActivityDurationTrack(false);
		//SIMMON_ADD
			new Thread(){
				@Override
				public void run(){
					try {
						getPlanData();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				handler.sendEmptyMessage(0);
				}
		}.start();
		 
	
		
		//返回主页按钮-------------------------------------------------------//
		 Button bBack = (Button) activity.findViewById(R.id.bBack_PlanWeek);
			
		 bBack.setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{	
				    activity.switchContentHome();
				}
			});
			
		//------------------------------------------------------------------//
		 
		 
		 
		 bGoWeek[0] = (ProgressBar) activity.findViewById(R.id.pbar_week1);
			
		 bGoWeek[0].setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					
					Plan_Day planDay = new Plan_Day(1);
					
					if (getActivity() instanceof MainActivity)
					{
						MainActivity activity = (MainActivity) getActivity();
						
						activity.switchContent(planDay);
					}
					
				}
			});
		 
		 
		 bGoWeek[1] = (ProgressBar) activity.findViewById(R.id.pbar_week2);
			
		 bGoWeek[1].setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					
					Plan_Day planDay = new Plan_Day(2);
					
					if (getActivity() instanceof MainActivity)
					{
						MainActivity activity = (MainActivity) getActivity();
						
						activity.switchContent(planDay);
					}
					
				}
			});
		 
		 
		 bGoWeek[2]= (ProgressBar) activity.findViewById(R.id.pbar_week3);
			
		 bGoWeek[2].setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					
					Plan_Day planDay = new Plan_Day(3);
					
					if (getActivity() instanceof MainActivity)
					{
						MainActivity activity = (MainActivity) getActivity();
						
						activity.switchContent(planDay);
					}
					
				}
			});
		 
		 bGoWeek[3] = (ProgressBar) activity.findViewById(R.id.pbar_week4);
			
		 bGoWeek[3].setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					
					Plan_Day planDay = new Plan_Day(4);
					
					if (getActivity() instanceof MainActivity)
					{
						MainActivity activity = (MainActivity) getActivity();
						
						activity.switchContent(planDay);
					}
					
				}
			});
		 
		 bGoWeek[4] = (ProgressBar) activity.findViewById(R.id.pbar_week5);
			
		 bGoWeek[4].setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					
					Plan_Day planDay = new Plan_Day(5);
					
					if (getActivity() instanceof MainActivity)
					{
						MainActivity activity = (MainActivity) getActivity();
						
						activity.switchContent(planDay);
					}
					
				}
			});
		 
		 bGoWeek[5] = (ProgressBar) activity.findViewById(R.id.pbar_week6);
			
		 bGoWeek[5].setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					
					Plan_Day planDay = new Plan_Day(6);
					
					if (getActivity() instanceof MainActivity)
					{
						MainActivity activity = (MainActivity) getActivity();
						
						activity.switchContent(planDay);
					}
					
				}
			});
		 
		 bGoWeek[6] = (ProgressBar) activity.findViewById(R.id.pbar_week7);
			
		 bGoWeek[6].setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					
					Plan_Day planDay = new Plan_Day(7);
					
					if (getActivity() instanceof MainActivity)
					{
						MainActivity activity = (MainActivity) getActivity();
						
						activity.switchContent(planDay);
					}
					
				}
			});
		 
		 
		 mProgressValue[0] = (TextView)activity.findViewById(R.id.textWeek1);
		 mProgressValue[1] = (TextView)activity.findViewById(R.id.textWeek2);
		 mProgressValue[2] = (TextView)activity.findViewById(R.id.textWeek3);
		 mProgressValue[3] = (TextView)activity.findViewById(R.id.textWeek4);
		 mProgressValue[4] = (TextView)activity.findViewById(R.id.textWeek5);
		 mProgressValue[5] = (TextView)activity.findViewById(R.id.textWeek6);
		 mProgressValue[6] = (TextView)activity.findViewById(R.id.textWeek7);
		 
		 
		 
		 /*
		 ImageView bGoDay = (ImageView) activity.findViewById(R.id.temp_planweek);
			
		 bGoDay.setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					
					Plan_Day planDay = new Plan_Day();
					
					if (getActivity() instanceof MainActivity)
					{
						MainActivity activity = (MainActivity) getActivity();
						
						activity.switchContent(planDay);
					}
					
				}
			});
			*/
	}
	
	
	
	/**
	 * 渲染界面,显示数据
	 */
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message paramMessage) {
			super.handleMessage(paramMessage);
			if(JSONResult!=null){
			BindWeekData(JSONResult);
			JSONResult =null;
			}else{
			  //  activity.openMainActivityDialog(); 
			}
		}
	};

	private void BindWeekData(String strJSON){
	    
		try{
				 JSONObject jsonObject = new JSONObject(strJSON);
				 String weekData = jsonObject.getString("result");
			     String[] weekDataArray = weekData.split(",");
			        for (int i = 0; i < weekDataArray.length; i++) {
			        	mProgress[i] =Integer.parseInt(weekDataArray[i]);

			        	bGoWeek[i].setProgress(mProgress[i]);
			        	String value =mProgress[i]+"%";
			        	mProgressValue[i].setText(value);
			        }
				  
    }
    catch(JSONException ex)
    {
    		Log.e("JSON Error: ", ex.toString()); 
    }
		
	}
	
	
	private void  getPlanData() throws IOException
	{
		//http://www.552120.com/API/Plan_GetHealthSum.aspx?userID=1&amp;planID=3
		String userID = "1";
		String planID = "3";
	
		String httpUrl = com.wehealth.util.GetUrl.Plan_GetHealthSum();
		 HttpPost httpRequest= new HttpPost(httpUrl);
		 
		 List <NameValuePair> params = new ArrayList <NameValuePair>();
		 
		 params.add(new BasicNameValuePair("userID",userID));
		 params.add(new BasicNameValuePair("planID",planID)); 
		 try
		 {
			 httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8) );
			
			 HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
			 
			 if(httpResponse.getStatusLine().getStatusCode() == 200)
			 {
				 JSONResult = EntityUtils.toString(httpResponse.getEntity());
			 }
		 }
		 catch(Exception e)
		 {
			
			 Log.e("error:",  e.toString());
			 
		 }

	}
	
	
    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        MobclickAgent.onPageEnd("Plan_Week"); 
    }


    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        MobclickAgent.onPageStart("Plan_Week"); 
    }
	
	
	
	
	
}
